<?php

require_once('logger.php');
require_once('sxml2array.php');


function    query_snap($mongo_addr, $urlmd5)
{
    $time_start = microtime(true);

    $connection = new Mongo($mongo_addr);

    $db = $connection->selectDB( 'snap' );

    $collection = $db->selectCollection( "news" );

    $data = False;
    $cursor = $collection->find( array( 'urlmd5' => $urlmd5 ) );
    while ($cursor->hasNext()) {
        $row = $cursor->getNext();
        //var_dump( $row );
        $data = $row[ 'data' ];
    }

    $result = False;
    $time_last = microtime(true) - $time_start;
    $logger = getLogger();

    global $_SERVER;
    $client_ip = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'unknown';

    if (empty($data)) {
        $logger->info(sprintf("client:%s status:fail urlmd5:%s time:%d",$client_ip,$urlmd5, intval($time_last * 1000)));
    }   
    else {
        $logger->info(sprintf("client:%s status:ok urlmd5:%s time:%d",$client_ip,$urlmd5, intval($time_last * 1000)));

        $sxml = gzuncompress(base64_decode($data));
        if (!empty($sxml)) {
            $res = sxml2array($sxml);
            if (!empty($res)) {
                $result = serialize($res);
            }   
        }   
    }   

    return $result;
}



